﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Glee.Drawing;
using RadowCore.Models;

namespace RadowCore.Helpers
{
    class PGViewer
    {
        private Graph pgGraph;
        private List<string> allEdges;
        private PGNode rootNode;
        private List<PGNode> drawedNodes;

        public List<PGNode> DrawedNodes
        {
            get { return drawedNodes; }
            set { drawedNodes = value; }
        }


        public PGViewer()
        {
            allEdges = new List<string>();
            drawedNodes = new List<PGNode>();
        }
        public Graph ColorNodes(List<PGNode> nodeList, Color c)
        {
            DrawGraph(this.rootNode);
           // foreach (PGNode node in nodeList)
            for(int i=0; i<nodeList.Count; i++)
            {
                pgGraph.FindNode(nodeList[i].Name).Attr.Fillcolor = c;
                if (i != nodeList.Count - 1)
                {
                    var e = pgGraph.Edges.Where(gd => gd.Source == nodeList[i].Name && gd.Target == nodeList[i + 1].Name );
                    
                        foreach(Microsoft.Glee.Drawing.Edge ed in e)
                            ed.Attr.Color = c;
                    
                }                
            }
            return pgGraph;
        }
        public Graph Clear()
        {
            if (pgGraph != null)
            {
                pgGraph = new Graph("PG");
                pgGraph.GraphAttr.LayerDirection = LayerDirection.LR;
                pgGraph.GraphAttr.NodeAttr.Shape = Shape.Box;
            }
            return pgGraph;
        }
        public Graph DrawGraph(PGNode fnode)
        {
            this.rootNode = fnode;
            pgGraph = new Graph("PG");
            pgGraph.GraphAttr.LayerDirection = LayerDirection.LR;
            pgGraph.GraphAttr.NodeAttr.Shape = Shape.Box;
            
            allEdges.Clear();
            DrawGraphRecursive(fnode);
            return pgGraph;
        }

        private void DrawGraphRecursive(PGNode fnode)
        {
            //Node existNode = pgGraph.FindNode(fnode.Name + fnode.Orignator);
            Node existNode = pgGraph.FindNode(fnode.Name );
            if (existNode == null)
            {
                //pgGraph.AddNode(fnode.Name+fnode.Orignator);
                pgGraph.AddNode(fnode.Name).Attr.Label = fnode.Name;
                Node n = pgGraph.FindNode(fnode.Name);
                n.Attr.Fillcolor = Color.LightGreen;
                drawedNodes.Add(fnode);
            }

            for (int i = 0; i < fnode.Edges.Count; i++)
            {
                //existNode = pgGraph.FindNode(fnode.Edges[i].TargetNode.Name + fnode.Edges[i].TargetNode.Orignator);
                existNode = pgGraph.FindNode(fnode.Edges[i].TargetNode.Name);
                if (existNode == null)
                {
                    //pgGraph.AddNode(fnode.Edges[i].TargetNode.Name + fnode.Edges[i].TargetNode.Orignator);
                    pgGraph.AddNode(fnode.Edges[i].TargetNode.Name).Attr.Label = fnode.Edges[i].TargetNode.Name;
                    drawedNodes.Add(fnode.Edges[i].TargetNode);
                    Node n = pgGraph.FindNode(fnode.Edges[i].TargetNode.Name);
                    n.Attr.Fillcolor = Color.LightGreen;
                    
                }
                //string evalue = fnode.Name + fnode.Orignator + fnode.Edges[i].Value + fnode.Edges[i].TargetNode.Name + fnode.Edges[i].TargetNode.Orignator;
                string evalue = fnode.Name+ fnode.Edges[i].Value + fnode.Edges[i].TargetNode.Name;                
                string existEdge = allEdges.Find(
                    delegate(string e)
                    {
                        return e == evalue;
                    }
                    );
                if (existEdge == null)
                {
                    //pgGraph.AddEdge(fnode.Name + fnode.Orignator, fnode.Edges[i].Value, fnode.Edges[i].TargetNode.Name + fnode.Edges[i].TargetNode.Orignator);
                    pgGraph.AddEdge(fnode.Name, fnode.Edges[i].Value, fnode.Edges[i].TargetNode.Name);
                    allEdges.Add(evalue);
                }
                
                DrawGraphRecursive(fnode.Edges[i].TargetNode);
            }
        }
    }
}
